import { System } from "csharp";

/**
 * 文件
 * @author clong 2020.11.29
 */
export default class File extends System.Object{

    /**
     * 获取文件名
     * @param url 
     */
    public static getFileName( url:string ):string{

        let val:string = url.substr( url.lastIndexOf("\\") );
        let isDirectory = url && url.indexOf(".") == -1;
        if( !isDirectory ) val = val.substr( 0 , val.lastIndexOf(".") );
        return val.replace(/\\/g,""); 
    }

    protected _url:string = "";

    /**根目录 */
    public root:File = null;
    /**当前目录下所有的目录及文件 */
    public files:Array<File> = null;

    /**目录列表信息 */
    public dirInfo:System.IO.DirectoryInfo = null;
    /**文件信息 */
    public fileInfo:System.IO.FileInfo = null;

    constructor( url:string , root:File = null ) {
        
        super();
        this._url = url;
        if( root == null ){
            this.root = this.isDirectory ? this : null;                        
        }else{
            this.root = root;
        }

        if(this.isDirectory){
            this.dirInfo = new System.IO.DirectoryInfo(url);
        }else{
            this.fileInfo = new System.IO.FileInfo( url );
        }

        this.files = this.getDirectoryListing();
    }

    public set url(value:string){
        this._url = value;
    }

    public get url():string{

        return this._url;
    }

    /**
     * 是否为目录
     */
    public get isDirectory():boolean{

        return this._url && this._url.indexOf(".") == -1;
    }

    /**
     * 所有子目录及文件
     */
    public getDirectoryListing():Array<File>{
        
        if( this.files == null ){
            this.files = [];
            if( this.isDirectory ){
                let file:File = null;
                let dirInfos = System.IO.Directory.GetDirectories(this._url);
                for(let i=0;i<dirInfos.Length;i++){
                    file = new File( dirInfos.get_Item(i) , this.root );
                    this.files.push( file );
                }
                let fileInfos = System.IO.Directory.GetFiles(this._url);
                for(let i=0;i<fileInfos.Length;i++){
                    file = new File( fileInfos.get_Item(i) , this.root );
                    this.files.push( file );
                }
            }
        }        
        return this.files;
    }

    /**
     * 文件名字或者当前目录名字
     */
    public get name():string{

        let val:string = this._url.substr( this._url.lastIndexOf("\\") );
        if( !this.isDirectory ) val = val.substr( 0 , val.lastIndexOf(".") );
        return val.replace(/\\/g,"");
    }

    /**
     * 文件后缀名
     */
    public get extension():string{

        let ext = this._url ? this._url.substr( this._url.lastIndexOf(".") + 1 ) : "";
        return ext;
    }

    public get isTxt():boolean{

        return this.extension == "txt" || this.extension == "json" || this.extension == "xml";
    }

    public get isXml():boolean{

        return this.extension == "xml";
    }

    public get isImg():boolean{

        return this.extension == "jpg" || this.extension == "png";
    }

    public get isBin():boolean{

        return this.extension == "bin" || this.extension == "bytes";
    }
}